package com.doit.demo01;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class teat02_condition {
    public static void main(String[] args) {
        Lock l = new ReentrantLock();
        Condition c1 = l.newCondition();//生产
        Condition c2 = l.newCondition();//消费
        List<String>list = new ArrayList<>();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true)
                {
                    l.lock();
                    if(list.size()!=0)
                    {
                        try {
                            c1.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    list.add("aaa");
                    System.out.println(list);
                    c2.signal();
//                    l.unlock();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true)
                {
                    l.lock();
                    if(list.size()==0)
                    {
                        try {
                            c2.await();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    list.remove(0);
                    System.out.println(list);
                    c1.signal();
//                    l.unlock();
                }
            }
        }).start();
    }
}
